package com.bxd.baodan.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.DesignCrInfoJSONBean;
import com.bxd.baodan.item.FamInfo;
import com.bxd.baodan.item.MemberInfo;
import com.bxd.baodan.util.GsonUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.widget.IosDialog;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class FamilyManagerActivity extends BaseActivity {
    private ListView lv;
    private List<FamInfo> famInfos = new ArrayList<>();

    private CommonAdapter<FamInfo> adapter;
    DesignCrInfoJSONBean bean;
    TextView tv;
    View no_report_ly, ljcp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_family_manager);
        lv = findViewById(R.id.lv);

        setActionBarTitle("家庭成员");
        initView();

        initAdapter();

        lv.setAdapter(adapter);
        lv.addFooterView(new View(this));
        lv.addHeaderView(new View(this));

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();

    }

    private void initAdapter() {
        adapter = new CommonAdapter<FamInfo>(this, famInfos, R.layout.family_manager_item_1) {
            @Override
            public void convert(final ViewHolder holder, FamInfo item) {
                View self_ly = holder.getView(R.id.self_ly);
                self_ly.setVisibility(View.GONE);
                View member_ly =holder.getView(R.id.member_ly);
                member_ly.setVisibility(View.GONE);

                if (item.roleName.equals("本人")) {
                    self_ly.setVisibility(View.VISIBLE);
                    member_ly.setVisibility(View.GONE);
                    ImageView iv = holder.getView(R.id.head_iv);
                    iv.setImageResource(item.drawable);

                } else {
                    self_ly.setVisibility(View.GONE);
                    member_ly.setVisibility(View.VISIBLE);
                    ImageView iv = holder.getView(R.id.iv);
                    TextView tv = holder.getView(R.id.role);
                    ImageView del = holder.getView(R.id.del);
                    iv.setImageResource(item.drawable);
                    tv.setText(item.roleName);

                    del.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            MobclickAgent.onEvent(App.getContext(), "v6_1_3_WD_jtcy_sc");

                            new IosDialog(FamilyManagerActivity.this).builder().setCancelable(false)
                                    .setTitle("提示")
                                    .setMsg("删除家庭成员需要重新确认家庭信息哦")

                                    .setPositiveButton("确定", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            bean.memberInfos.remove(holder.getPosition());
                                            String data = GsonUtil.toJson(bean);
                                            openActivity(DelMemeberActivity.class, new String[]{"fam_info"}, new String[]{data});

                                        }
                                    })
                                    .setNegativeButton("取消", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {

                                        }
                                    }).show();

                        }
                    });
                }


            }
        };
    }

    private void initView() {
        tv = findViewById(R.id.tv_bar_right);
        tv.setText("添加");
        tv.setTextColor(getResources().getColor(R.color.green_lab));



        no_report_ly = findViewById(R.id.no_report_ly);
        ljcp = findViewById(R.id.ljcp);
        ljcp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(FamilyDesignActivity.class);
            }
        });

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MobclickAgent.onEvent(App.getContext(), "v6_1_3_WD_jtcy_tj");
                String data = GsonUtil.toJson(bean);
                openActivity(AddMemeberActivity.class, new String[]{"fam_info"}, new String[]{data});
            }
        });
    }

    private void getData() {
        NetUtil netUtil = new NetUtil(this, JsonApi.DESIGN_FAMILYDESIGN);
        netUtil.postRequest("...", new NetUtil.DataListener() {
            @Override
            public void onComplete(String data) {

                famInfos.clear();


                String jsonStr = JSONUtil.getData("cr_infoJSON", data);

                bean = (DesignCrInfoJSONBean) GsonUtil.fromJson(jsonStr, DesignCrInfoJSONBean.class);
                if (bean != null && bean.familyInfo != null) {

                    String familyInfoStr = bean.familyInfo;
                    List<MemberInfo> familyInfo = GsonUtil.fromJsonList(familyInfoStr, MemberInfo.class);
                    bean.memberInfos = familyInfo;
                    int childTotalCount = 0;
                    for (int i = 0; i < bean.memberInfos.size(); i++) {
                        MemberInfo info = bean.memberInfos.get(i);
                        if (info.role.equals("child")) {
                            childTotalCount++;
                        }

                    }

                    // FIXME: 18/1/17 有序添加,才能根据顺序删除对应数据

                    int count = 0;
                    for (int i = 0; i < bean.memberInfos.size(); i++) {
                        MemberInfo info = bean.memberInfos.get(i);
                        FamInfo famInfo = new FamInfo();
                        String role = "";
                        if (info.role.equals("my")) {

                            famInfo.roleName = "本人";
                            if (info.gender.equals("female")) {
                                famInfo.drawable = R.drawable.woman;
                            } else {
                                famInfo.drawable = R.drawable.man;
                            }
                            famInfos.add(famInfo);

                        } else if (info.role.equals("spouse")) {
                            famInfo.roleName = "配偶";
                            if (info.gender.equals("female")) {
                                famInfo.drawable = R.drawable.woman;
                            } else {
                                famInfo.drawable = R.drawable.man;
                            }
                            famInfos.add(famInfo);
                        } else if (info.role.equals("father")) {
                            famInfo.roleName = "父亲";
                            famInfo.drawable = R.drawable.father;
                            famInfos.add(famInfo);
                        } else if (info.role.equals("mother")) {
                            famInfo.roleName = "母亲";
                            famInfo.drawable = R.drawable.mother;
                            famInfos.add(famInfo);
                        } else if (info.role.equals("child")) {
                            count++;
                            if (info.gender.equals("female")) {
                                famInfo.drawable = R.drawable.girl;
                            } else {
                                famInfo.drawable = R.drawable.boy;
                            }
                            if (count == 1) {
                                if (childTotalCount == count) {
                                    famInfo.roleName = "孩子";
                                    famInfos.add(famInfo);
                                } else {
                                    famInfo.roleName = "第一个孩子";
                                    famInfos.add(famInfo);
                                }

                            } else if (count == 2) {
                                famInfo.roleName = "第二个孩子";
                                famInfos.add(famInfo);
                            } else if (count == 3) {
                                famInfo.roleName = "第三个孩子";
                                famInfos.add(famInfo);
                            }


                        }
                    }

                } else {

                }
                if (adapter.getCount() == 0) {
                    no_report_ly.setVisibility(View.VISIBLE);
                    tv.setVisibility(View.GONE);
                } else {
                    no_report_ly.setVisibility(View.GONE);
                    tv.setVisibility(View.VISIBLE);
                }


                adapter.notifyDataSetChanged();

            }
        });


    }
}
